"use strict";

$(function () {
    $(".modal .close").click(function () {
        $(".modal").addClass("hide");
    });
    loadList();
    clickUser();

    //加载列表
    function loadList() {
        $.ajax({
            type: "post",
            url: "/wzmanage/res/list",
            contentType: "application/json;charset=utf-8",
            success: function success(data) {
                console.log(data);
                var html = '';
                if (data instanceof Array && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        html += "<tr>\n                            \n                                    <td class=\"name\">\n                                       <span>" + data[i].name + "</span>\n                                        <input class=\"form-control id\"  type=\"hidden\" value=\"" + data[i].id + "\">\n                                    </td>\n                                    <td class=\"url\">" + data[i].url + "</td>\n                                    <td class=\"description\">" + data[i].description + "</td> \n                                    <td>\u6682\u65E0</td>         \n                                </tr>";
                    }
                } else {
                    html = "<tr><td colspan=\"4\">\u6682\u65E0\u8D44\u6E90\u7BA1\u7406</td></tr>";
                }
                $(".user-list tbody").html(html);
            }
        });
    };
    //用户增删改列表
    $(".add-del-person").on("click", "a", function (e) {
        e.preventDefault();
        var index = $(this).index();
        if (index == 0) {
            addHtml();
            $(".new-add").removeClass("hide");
            addUser();
        } else if (index == 1) {
            allUserInfo();
        } else if (index == 2) {
            deluserInfo();
        }
        console.log($(this).index());
    });

    //删除
    function deluserInfo() {
        var tdinfo = $(".user-list tbody tr.active");
        console.log(tdinfo);
        var id = tdinfo.find(".id").val();
        console.log(id);
        if (id) {
            if (confirm('确定要删除？')) {
                $.ajax({
                    url: "/wzmanage/res/delete/" + id,
                    success: function success(data) {
                        console.log(data);
                        if (data == "OK") {
                            $(".new-add").addClass("hide");
                            $(".add-success").css("display", "block");
                            $(".add-success h3").html("删除资源成功");
                            setTimeout(function () {
                                $(".add-success").fadeOut();
                            }, 500);
                            loadList();
                        }
                    },
                    error: function error(data) {
                        console.log(data);
                    }
                });
            }
        }
    }

    //新员操作
    function addUser() {
        $(".new-add .save").unbind();
        $(".new-add .save").click(function () {
            if (isEmpty() == 1) return;
            console.log(JSON.stringify($(".user-form").serializeObject()));
            $.ajax({
                url: "/wzmanage/res/add",
                type: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify($(".user-form").serializeObject()),
                success: function success(data) {
                    if (data == "OK") {
                        $(".new-add").addClass("hide");
                        $(".add-success h3").html("新增资源成功");
                        $(".add-success").css("display", "block");
                        setTimeout(function () {
                            $(".add-success").fadeOut();
                        }, 500);
                        loadList();
                    }
                },
                error: function error(data) {
                    console.log(data);
                }
            });
        });
    }

    //选中人员操作
    function clickUser() {
        //点击选中人员所在的行
        $(".user-list tbody").on("click", 'tr', function () {
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active").siblings(".active").removeClass("active");
            }
        });
    }

    //获得所在的行人员信息
    function allUserInfo() {
        $(".new-add .save").unbind();
        //把人员信息添加到表当中
        var tdinfo = $(".user-list tbody tr.active");
        if (tdinfo.length) {
            var html = " \n \t\t\t<tr>\n\t\t\t\t<td>\u540D\u79F0:</td>\n\t\t\t\t<td><input class=\"form-control\" name=\"name\" type=\"text\" value=\"" + $(tdinfo).find("span").html() + "\">\n\t\t\t\t<input type=\"hidden\" name=\"id\" value=\"" + tdinfo.find(".id").val() + "\">\n\t\t\t\t</td>\n\t\t\t\t\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>url:</td>\n\t\t\t\t<td><input class=\"form-control\" name=\"url\" type=\"text\" value=\"" + tdinfo.find(".url").html() + "\"></td>\n\t\t\t</td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>\u63CF\u8FF0:</td>\n\t\t\t\t<td>\n\t\t\t\t\t<textarea class=\"form-control resosurce\" name=\"description\" >" + tdinfo.find(".description").html() + "</textarea>\n\t\t\t\t</td>\n\t\t\t\t\n\t\t\t</tr>";
            $(".user-form table tbody").html(html);
            $(".new-add").removeClass("hide");
            fixUser();
        }
    }

    //修改信息
    function fixUser() {
        $(".new-add .save").click(function () {
            if (isEmpty() == 1) return;
            $.ajax({
                url: "/wzmanage/res/update",
                type: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify($(".user-form").serializeObject()),
                success: function success(data) {
                    console.log("update" + data);
                    if (data == "OK") {
                        $(".new-add").addClass("hide");
                        $(".add-success h3").html("修改资源成功");
                        $(".add-success").css("display", "block");
                        setTimeout(function () {
                            $(".add-success").fadeOut();
                        }, 500);
                        loadList();
                    }
                },
                error: function error(data) {
                    console.log(data);
                }
            });
        });
    }

    //新增资源信息html
    function addHtml() {
        var html = "<tr>\n                                    <td>\u540D\u79F0:</td>\n                                    <td><input class=\"form-control\" name=\"name\" type=\"text\"></td>\n\n                                </tr>\n                                <tr>\n                                    <td>url:</td>\n                                    <td><input class=\"form-control\" name=\"url\" type=\"text\"></td>\n                                </tr>\n                                <tr>\n                                    <td>\u63CF\u8FF0:</td>\n                                    <td>\n                                        <textarea  class=\"form-control resosurce\" name=\"description\" ></textarea>\n                                    </td>\n\n                                </tr>";
        $(".user-form table tbody").html(html);
    }

    //验证非空
    function isEmpty() {
        var inps = $(".new-add").find("input,textarea");
        for (var i = 0; i < inps.length; i++) {
            if (!inps[i].value) {
                inps[i].focus();
                $(inps[i]).attr("placeholder", "不能为空");
                return 1;
            }
        }
    }

    //插件
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
});